
//
//  SetMonthDestinationViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/27.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "SetMonthDestinationViewController.h"

@interface SetMonthDestinationViewController ()<UITextViewDelegate>
@property (nonatomic,weak) JSTextView *releaseContentTextView;
@property (nonatomic,weak) JSTextView *releaseZJContentTextView;
@property (nonatomic,weak) UIButton *selectButton;
@property (nonatomic,assign) BOOL isEdit;   //是否可编辑
@property (nonatomic,assign) BOOL isStatus; //是否完成目标
@end

@implementation SetMonthDestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"月目标月总结";
    NSString *statusStr = [NSString stringWithFormat:@"%@",self.monthDesDic[@"status"]];
    self.isStatus = ![statusStr isEqualToString:@"0"];
    if (!self.isStatus) {
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithTitle:@"编辑" target:self selector:@selector(editDestination)];
    }
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.isEdit = NO;
//    self.isEdit = self.monthDesDic[@"status"]==0; //完成状态 0未完成 1 已完成

    UIView *footView = [[UIView alloc]init];
    footView.frame = CGRectMake(0, 0, kScreenW, 100);
    footView.backgroundColor = [UIColor whiteColor];
    
    CompleteButton *completeButton = [[CompleteButton alloc]initWithFrame:CGRectMake(10, 10, kScreenW - 20, 50) bgColor:KBACKNAVIGATIONCOLOR tColor:[UIColor whiteColor] text:@"提交"];
    [completeButton addTarget:self action:@selector(reportMonthDestination) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:completeButton];
    self.tableView.tableFooterView = footView;
}
#pragma mark - 提交月目标月总结
- (void)reportMonthDestination{
    [self.tableView endEditing:YES];
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:self.releaseContentTextView.text forKey:@"context"];   //内容
    [dic setValue:self.releaseZJContentTextView.text forKey:@"context_zj"];   //总结
    
    [dic setValue:[NSString stringWithFormat:@"%@-%@",self.year,self.monthDesDic[@"mouth"]] forKey:@"date"];   //日期
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppSummaryAddUserGoals] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [Global showActivityWithView:self.view withText:@"提交成功"];
            if (self.sendMonthDestinationBlock) {
                self.sendMonthDestinationBlock();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section) {
        return 1;
    }
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!indexPath.section && indexPath.row) {
        static NSString *CellIdentifier = @"Cell01";
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                          reuseIdentifier: CellIdentifier];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
            
            UILabel *sepLabel = [[UILabel alloc]init];
            sepLabel.frame = CGRectMake(10, 10, kScreenW - 20, 2);
            sepLabel.backgroundColor = KBACKGROUNDCOLOR;
            [cell.contentView addSubview:sepLabel];
            
            UILabel *stateLabel = [[UILabel alloc]init];
            stateLabel.text = @"目标状态";
            stateLabel.textColor = KTEXTMAINCOLOR;
            stateLabel.textAlignment = NSTextAlignmentCenter;
            stateLabel.backgroundColor = [UIColor whiteColor];
            stateLabel.frame = CGRectMake((kScreenW - 100)/2, 0, 100, 20);
            [cell.contentView addSubview:stateLabel];
            
            for (int i = 0; i < 2; i ++) {
                CustomeButton *customeButton = [[CustomeButton alloc]initWithRelativeAlignment:KRelativeAlignmentDown titleNormal:i==0?@"未完成":@"完成" imageNormal:i==0?@"month_fail":@"month_complete" frame:CGRectMake((kScreenW - 60*3)/2 + (60 + 60 )*i , 30, 60, 70) titleRect:CGRectMake( 0, 50, 60, 20) imageRect:CGRectMake( 9, 0, 42, 42) textAlignment:NSTextAlignmentCenter font:[UIFont systemFontOfSize:14] titleColor:KTEXTMAINCOLOR];
                customeButton.tag = 1000+i;
                [customeButton addTarget:self action:@selector(selectDest:) forControlEvents:UIControlEventTouchUpInside];
                [cell.contentView addSubview:customeButton];
            }
        }
        CustomeButton *button = [cell.contentView viewWithTag:1000];
        [button setImage:[UIImage imageNamed:@"month_high_fail"] forState:UIControlStateSelected];
        button.userInteractionEnabled = self.isEdit;
        button.selected = !self.isStatus;
        self.selectButton = button;
        
        CustomeButton *comButton = [cell.contentView viewWithTag:1001];
        [comButton setImage:[UIImage imageNamed:@"month_high_complete"] forState:UIControlStateSelected];
        comButton.userInteractionEnabled = self.isEdit;
        comButton.selected = self.isStatus;
        return cell;
    }
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    JSTextView *releaseContentTextView = [[JSTextView alloc] initWithFrame:CGRectMake(10, 10, kScreenW - 20, 80)];
    releaseContentTextView.font = [UIFont systemFontOfSize:16];
    releaseContentTextView.textColor = KTEXTMAINCOLOR;
    releaseContentTextView.autocorrectionType = UITextAutocorrectionTypeNo;  //取消纠错
    releaseContentTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;  //取消首字母大写
    releaseContentTextView.returnKeyType = UIReturnKeyDefault;  //return改成done
    [cell.contentView addSubview:releaseContentTextView];
    if (!indexPath.section) {
        releaseContentTextView.text = strIsEmpty(self.monthDesDic[@"context"])?@"":self.monthDesDic[@"context"];
        self.releaseContentTextView = releaseContentTextView;
    }else{
        releaseContentTextView.text = strIsEmpty(self.monthDesDic[@"context_zj"])?@"":self.monthDesDic[@"context_zj"];
        self.releaseZJContentTextView = releaseContentTextView;
    }
    releaseContentTextView.myPlaceholder = indexPath.section? @"这里是总结" : @"这里是写目标";
    releaseContentTextView.userInteractionEnabled = self.isEdit;
    return cell;
}
- (void)selectDest:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    if (self.selectButton != sender) {
        self.selectButton.selected = NO;
        self.selectButton = sender;
    }
    if ( sender.tag-1000==1 && self.selectButton.selected) {
        YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
        [dic setValue:self.monthDesDic[@"id"] forKey:@"id"];   //查出的目标列表某条记录返
        [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppSummaryUpdateUserGoals] andParmas:dic andComplition:^(id response, BOOL isuccess) {
            if (isuccess) {
                if (self.sendMonthDestinationBlock) {
                    self.sendMonthDestinationBlock();
                }
//                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
    
    MyLog(@"%ld %@",(long)self.selectButton.tag,self.selectButton.selected?@"yes":@"no");
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!indexPath.row) {
        return 100;
    }
    return 114;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 44);
    view.backgroundColor = [UIColor whiteColor];
    
    UIView *sepView = [[UIView alloc]init];
    sepView.backgroundColor = KBACKNAVIGATIONCOLOR;
    sepView.frame = CGRectMake(0, 12, 2, 20);
    [view addSubview:sepView];
    
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(15, 12, 100, 20);
    label.font = [UIFont systemFontOfSize:16];
    if (section) {
        label.text = @"写总结";
    }else{
        label.text = [NSString stringWithFormat:@"%@月目标",self.monthDesDic[@"mouth"]];
    }
    
    [view addSubview:label];
    
    UILabel *sepLabel = [[UILabel alloc]init];
    sepLabel.frame = CGRectMake(0, 43, kScreenW , 1);
    sepLabel.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:sepLabel];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = KBACKGROUNDCOLOR;
    if (!section) {
        view.frame = CGRectMake(0, 0, kScreenW, 10);
        return view;
    }
    view.frame = CGRectZero;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (!section) {
        return 10.01f;
    }
    return 0.01f;
}
#pragma mark - 编辑月目标月总结
- (void)editDestination{
    MyLog(@"%ld %@",(long)self.selectButton.tag,self.selectButton.selected?@"yes":@"no");
    if (self.selectButton.tag==1001 && self.selectButton.selected) {
        [Global showActivityWithView:self.view withText:@"目标为已完成状态，不能编辑"];
        self.isEdit = NO;
    }else{
        self.isEdit = YES;
    }
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
